// pages/result/result.js
const app = getApp()
import { addBlood } from '../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bloodPressureOpen: '', // 低压
    bloodPressureClose: '', // 高压
    pulse: '', // 心率
    arrhythmia: '', // 心率失常
    equipment_version: '', // 设备版本
    type: '',
    measureTime: '', // 测量时间
    equipmentNo: '' // 设备编号
  },

  bindViewTap02: function () {
    var e = this;
    "" != e.data.deviceId && null != e.data.deviceId ? (wx.openBluetoothAdapter({
      success: function (e) {
        console.log(e), 
        wx.startBluetoothDevicesDiscovery({
          services: [],
          success: function (e) {
            console.log(e);
          },
          fail: function (e) {
            console.log(e);
          },
          complete: function (e) {
            console.log(e);
          }
        });
      },
      fail: function (e) {
        console.log(e);
      },
      complete: function (e) {
        console.log(e);
      }
    }), 
    wx.redirectTo({
      url: "../celiang/celiang?deviceId=" + e.data.deviceId,
      success: function (e) { },
      fail: function (e) { },
      complete: function (e) { }
    })) : wx.redirectTo({
        url: "../jinrixueya/jinrixueya"
    });
  },
  onLoad: function (o) {
    var t = this;
    wx.hideShareMenu(), 
    console.log("deviceId=" + o.deviceId),
    console.log("bloodPressureOpen=" + o.bloodPressureOpen),
    console.log("bloodPressureClose=" + o.bloodPressureClose),
    console.log("pulse=" + o.pulse), 
    console.log("arrhythmia=" + o.arrhythmia), 
    console.log("equipment_version=" + o.equipment_version),
    console.log("equipmentNo=" + o.equipmentNo), 
    t.setData({
      deviceId: o.deviceId,
      bloodPressureOpen: o.bloodPressureOpen,
      bloodPressureClose: o.bloodPressureClose,
      pulse: o.pulse,
      arrhythmia: o.arrhythmia,
      equipment_version: o.equipment_version,
      equipmentNo: o.equipmentNo,
      type: o.type,
      measureTime: o.measureTime
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  // 保存
  subClick() {
    let _this = this
    let form = {}
    let times = new Date(_this.data.measureTime).getHours()
    if (times < 10) {
      form = {
        zao_low_blood: _this.data.bloodPressureOpen,
        zao_high_blood: _this.data.bloodPressureClose,
        zhong_low_blood: '',
        zhong_high_blood: '',
        wan_low_blood: '',
        wan_high_blood: ''
      }
    } else if (10 <= times && times <= 15) {
      form = {
        zao_low_blood: '',
        zao_high_blood: '',
        zhong_low_blood: _this.data.bloodPressureOpen,
        zhong_high_blood: _this.data.bloodPressureClose,
        wan_low_blood: '',
        wan_high_blood: ''
      }
    } else {
      form = {
        zao_low_blood: '',
        zao_high_blood: '',
        zhong_low_blood: '',
        zhong_high_blood: '',
        wan_low_blood: _this.data.bloodPressureOpen,
        wan_high_blood: _this.data.bloodPressureClose
      }
    }
    form.openid = app.globalData.openid
    addBlood(form).then(res => {
      if (res.code == 200) {
        wx.showToast({
          title: '保存成功',
          icon: 'success',
          duration: 1500
        })
        setTimeout(() => {
          wx.redirectTo({
            url: "../jinrixueya/jinrixueya"
          })
        }, 1500)
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})